#pragma once 
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <fstream>
#include <atomic>
#include <vector>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    const std::string path = "./temp/";

    class TimeUtil
    {
    public:
        static std::string getTimeStemp()
        {
            struct timeval t;
            gettimeofday(&t, nullptr);
            return std::to_string(t.tv_sec);
        }
        static std::string getTimeMs()
        {
            struct timeval t;
            gettimeofday(&t, nullptr);
            return std::to_string(t.tv_sec*1000 + t.tv_usec/1000);
        }
    };
    class PathUtil
    {
    public:
        static std::string Suffix(const std::string& file_name, const std::string& suffix)
        {
            std::string path_name = path;
            path_name += file_name;
            path_name += suffix;
            return path_name;
        }
        // 编译的
        static std::string Src(const std::string& file_name)
        {
            return Suffix(file_name, ".cpp");
        }
        static std::string Exe(const std::string& file_name)
        {
            return Suffix(file_name, ".exe");
        }
        static std::string Compile_Error(const std::string& file_name)
        {
            return Suffix(file_name, ".compile_err");
        }
        // 运行的
        static std::string Stdin(const std::string& file_name)
        {
            return Suffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string& file_name)
        {
            return Suffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string& file_name)
        {
            return Suffix(file_name, ".stderr");
        }
    };

    class FileUtil
    {
    public:
        static bool IsFileExist(const std::string& path_name)
        {
            struct stat st;
            if(stat(path_name.c_str(), &st) == 0)
            {
                return true;
            }
            return false;
        }
        static std::string UniqueFile()
        {
            static std::atomic_uint id(0);
            id++;
            std::string name = TimeUtil::getTimeMs();
            name += std::to_string(id);
            return name;
        }
        static bool WriteFile(const std::string& target, const std::string& content)
        {
            std::ofstream out(target);
            if(!out.is_open())
            {
                return false;
            }
            out.write(content.c_str(), content.size());
            out.close();
            return true;
        }
        static bool ReadFile(const std::string& target, std::string* content)
        {
            (*content).clear();
            std::ifstream in(target);
            if(!in.is_open())
            {
                return false;
            }
            std::string line;
            while(getline(in, line))
            {
                *content += line;
                *content += "\n";
            }
            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void splitString(const std::string& str, std::vector<std::string>* v, const std::string& sep)
        {
            boost::split(*v, str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}